<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Using Java Arrays</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Scheme-types-in-Java.xhtml" title="Scheme types in Java"/>
    <link rel="next" href="Loading-Java-functions-into-Scheme.xhtml" title="Loading Java functions into Scheme"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Using Java Arrays" epub:type="subchapter" id="Array-operations">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Using Java Arrays</h2>
          </div>
        </div>
      </div>
      <span id="Creating-new-Java-arrays"/>
      <section class="sect2" title="Creating new Java arrays" epub:type="division" id="idm139667870790912">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Creating new Java arrays</h3>
            </div>
          </div>
        </div>
        <p>To allocate a Java array you can use the array type specifier
as a constructor function.  For example, to allocate an array with room for 10 elements
each of each is a primitive <code class="literal">int</code>:
</p>
        <pre class="screen">(int[] length: 10)
</pre>
        <p>You can specify the initial elements instead of the length:
</p>
        <pre class="screen">(object[] 31 32 33 34)
</pre>
        <p>This creates a 4-length array, initialized to the given values.
</p>
        <p>Note this is a variation of the generation object-allocation
(see <a class="link" href="Allocating-objects.xhtml" title="Allocating objects">Allocating objects</a>) pattern.  You can explicitly
use the <code class="literal">make</code> function, if you prefer:
</p>
        <pre class="screen">(make object[] 31 32 33 34)
</pre>
        <p>If you specify a length, you can also specify initial values for selected
elements.
If you specify an index, in the form of a literal integer-valued keyword,
then following elements are placed starting at that position.
</p>
        <pre class="screen">(int[] length: 100 10 12 80: 15 16 50: 13 14)
</pre>
        <p>This creates an array with 100 elements.  Most of them are initialized
to the default value of zero,
but elements with indexes 0, 1, 50, 51, 80, 81 are initialized
to the values 10, 12, 13, 14, 15, 16, respectively.
</p>
      </section>
      <section class="sect2" title="Accessing Java array elements" epub:type="division" id="idm139667870784080">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Accessing Java array elements</h3>
            </div>
          </div>
        </div>
        <p>You can access the elements of a Java array by treating it as
a one-argument function, where the argument is the index:
</p>
        <pre class="screen">(define primes (integer[] 2 3 5 7 11 13))
(primes 0) ⇒ 2
(primes 5) ⇒ 13
</pre>
        <p>You can set an element by treating the array as a function
with a <code class="literal">setter</code>:
</p>
        <pre class="screen">(set! (primes 0) -2)
(set! (primes 3) -7)
primes ⇒ [-2 3 5 -7 11 13]
</pre>
        <p>To get the number of elements of an array, you can treat
it as having a <code class="literal">length</code> field:
</p>
        <pre class="screen">primes:length ⇒ 6
</pre>
        <p>Here is a longer example.  This is the actual definition of the
standard <code class="literal">gcd</code> function.  Note the <code class="literal">args</code> variable
receives all the arguments on the form of an <code class="literal">integer</code> array.
(This uses the Java5 varargs feature.)
</p>
        <pre class="screen">(define (gcd #!rest (args ::integer[])) ::integer
  (let ((n ::int args:length))
    (if (= n 0)
	0
	(let ((result ::integer (args 0)))
	  (do ((i ::int 1 (+ i 1)))
	      ((&gt;= i n) result)
	    (set! result (gnu.math.IntNum:gcd result (args i))))))))
</pre>
        <p>The above example generates good code,
thanks to judicious use of casts and type specifications.
In general, if Kawa knows that
a “function” is an array then it will generate efficient
bytecode instructions for array operations.
</p>
      </section>
      <section class="sect2" title="Old low-level array macros" epub:type="division" id="idm139667870776432">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Old low-level array macros</h3>
            </div>
          </div>
        </div>
        <p>The deprecated <a class="link" href="Low-level-functions.xhtml#Low-level-array-macros">Low-level array macros</a> are also supported.
</p>
      </section>
    </section>
    <footer>
      <div class="navfooter">
        <ul>
          <li>
            <b class="toc">
              <a href="Array-operations.xhtml#idm139667870790912">Creating new Java arrays</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Array-operations.xhtml#idm139667870784080">Accessing Java array elements</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Array-operations.xhtml#idm139667870776432">Old low-level array macros</a>
            </b>
          </li>
        </ul>
        <p>
          Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
        <p>
        Previous: <a accesskey="p" href="Scheme-types-in-Java.xhtml">Scheme types in Java</a></p>
        <p>
        Next: <a accesskey="n" href="Loading-Java-functions-into-Scheme.xhtml">Loading Java functions into Scheme</a></p>
      </div>
    </footer>
  </body>
</html>
